package com.reggie.mvp_demo.model;
import android.app.Service;
import android.util.Log;
import com.reggie.mvp_demo.MainContract;
import com.reggie.mvp_demo.bean.Event;
import com.reggie.mvp_demo.callback;

import org.reactivestreams.Subscriber;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.fastjson.FastJsonConverterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


public class Model implements MainContract.ModelInterface {

    API.Service Service = null;
    //事件数据
    private List<Event> events = new ArrayList<>();
    private MainContract.MainPresenterInterface presenter;

    public Model(MainContract.MainPresenterInterface presenter) {

        this.presenter = presenter;
    }

    public void getTodayEvent(String v, int m, int d,final callback callback) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(API.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                //支持rxjava
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        //创建Service实体类，创建完后service就可以调用api里写的方法了
        Service = retrofit.create(API.Service.class);
        Service.getTodayEvent(API.APPKEY,v,m,d )
                .subscribeOn(Schedulers.io())//请求数据的事件发生在io线程
                .observeOn(AndroidSchedulers.mainThread())//请求完成后在主线程更显UI
                .subscribe(new DisposableObserver<Event>() {
                    @Override
                    public void onNext(Event event) {

                         events.add(event);

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        callback.LoadSuccess(events);
                    }
                });

    }
}

